/**
 * @author Vico @vicocotea
 * original filter: https://github.com/evanw/glfx.js/blob/master/src/filters/adjust/noise.js
 */

/**
 * A Noise effect filter.
 * 
 * @class NoiseFilter
 * @extends AbstractFilter
 * @constructor
 */
PIXI.FilmFilter = function()
{
    PIXI.AbstractFilter.call( this );

    this.passes = [this];

    // set the uniforms
   /*this.uniforms = {
        noise: {type: '1f', value: 0.5}
    };*/


    this.uniforms = {

       // "tDiffuse":   { type: "t", value: null },
        
        "nIntensity": { type: "1f", value: 0.8 },
        "sIntensity": { type: "1f", value: 0.1},
        "sCount":     { type: "1f", value: 24},
        "time":       { type: "1f", value: 0.0 },
      

        noise: {type: '1f', value: 0.5}
    };

    this.fragmentSrc = [
        'precision mediump float;',
        'uniform sampler2D uSampler;',
        /*'uniform float noise;',*/
        'varying vec2 vTextureCoord;',

        // noise effect intensity value (0 = no effect, 1 = full effect)
        "uniform float nIntensity;",

        // scanlines effect intensity value (0 = no effect, 1 = full effect)
        "uniform float sIntensity;",

        // scanlines effect count value (0 = no effect, 4096 = full effect)
        "uniform float sCount;",
        "uniform float time;",
      
        /*'void main() {',
        '    vec4 color = texture2D(uSampler, vTextureCoord);',
            
       // '    float diff = (rand(vTextureCoord) - 0.5) * noise;',
        '    float diff = 0.0;',
        '    color.r += diff;',
        '    color.g += diff;',
        '    color.b += diff;',
            
        '    gl_FragColor = color;',
        '}'*/



       "void main() {",

            // sample the source
            "vec4 cTextureScreen = texture2D( uSampler, vTextureCoord );",
            "float sIntensity2 = sIntensity;",
            // make some noise
            "float x = vTextureCoord.x * vTextureCoord.y * time *  1000.0;",
            "x = mod( x, 13.0 ) * mod( x, 123.0 );",
            "float dx = mod( x, 0.01 );",

            // add noise
            "vec3 cResult = cTextureScreen.rgb + cTextureScreen.rgb * clamp( 0.1 + dx * 100.0, 0.0, 1.0 );",

            // get us a sine and cosine
            "vec2 sc = vec2( sin( vTextureCoord.y * sCount ), cos( vTextureCoord.y * sCount + time / 50.0) );",
            "if(mod(time, 123.0) == 0.0)",
            "{",
                "sc.y = 0.0;",
                "sIntensity2 = 1.0;",
            "}",


            // add scanlines
            "cResult += cTextureScreen.rgb * vec3( sc.x, sc.y , sc.x ) * sIntensity2;",

            // interpolate between source and result by intensity
            "cResult = cTextureScreen.rgb + clamp( nIntensity, 0.0,1.0 ) * ( cResult - cTextureScreen.rgb );",

            "gl_FragColor =  vec4( cResult, cTextureScreen.a );",

        "}"

    ];
};

PIXI.FilmFilter.prototype = Object.create( PIXI.AbstractFilter.prototype );
PIXI.FilmFilter.prototype.constructor = PIXI.FilmFilter;

/**
 * The amount of noise to apply.
 * @property noise
 * @type Number
*/


Object.defineProperty(PIXI.FilmFilter.prototype, 'time', {
    get: function() {
        return this.uniforms.time.value;
    },
    set: function(value) {
        this.dirty = true;
        this.uniforms.time.value = value;
    }
});


Object.defineProperty(PIXI.FilmFilter.prototype, 'nIntensity', {
    get: function() {
        return this.uniforms.nIntensity.value;
    },
    set: function(value) {
        this.dirty = true;
        this.uniforms.nIntensity.value = value;
    }
});


Object.defineProperty(PIXI.FilmFilter.prototype, 'sIntensity', {
    get: function() {
        return this.uniforms.sIntensity.value;
    },
    set: function(value) {
        this.dirty = true;
        this.uniforms.sIntensity.value = value;
    }
});


Object.defineProperty(PIXI.FilmFilter.prototype, 'sCount', {
    get: function() {
        return this.uniforms.sCount.value;
    },
    set: function(value) {
        this.dirty = true;
        this.uniforms.sCount.value = value;
    }
});

